package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.ShopCommodityImageMapper;
import com.woniuxy.common.mapper.ShopCommodityMapper;
import com.woniuxy.common.mapper.ShopMerchantMapper;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.MerchantException;
import com.woniuxy.joylifeshop.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    ShopCommodityMapper shopCommodityMapper;

    @Autowired
    ShopMerchantMapper shopMerchantMapper;

    @Autowired
    ShopCommodityImageMapper shopCommodityImageMapper;
    
    /**
    * @Author SooYoo
    * @Description 通过item列表找到商家列表,商品列表,商品图片
    * @Param 
    * @Return 
    **/
    @Override
    public ObjectDto findMerchants(List<ShopOrderItem> shopOrderItems) {

        ObjectDto objectDto = new ObjectDto();

        try {
            //商品集合
            List<ShopCommodity> shopCommodities = new ArrayList<>();
            //商家集合
            List<ShopMerchant> shopMerchants = new ArrayList<>();
            //商品图片集合
            List<ShopCommodityImage> shopCommodityImages = new ArrayList<>();
            for (ShopOrderItem shopOrderItem:shopOrderItems){
               ShopCommodity shopCommodity = shopCommodityMapper.selectByPrimaryKey(shopOrderItem.getComId());
                ShopMerchant shopMerchant = shopMerchantMapper.selectByPrimaryKey(shopCommodity.getMerchantId());
                ShopCommodityImage shopCommodityImage = shopCommodityImageMapper.selectByPrimaryKey(shopCommodity.getImgId());
                shopMerchants.add(shopMerchant);
                shopCommodities.add(shopCommodity);
                shopCommodityImages.add(shopCommodityImage);
            }

            objectDto.setData(shopCommodities);
            objectDto.setData2(shopMerchants);
            objectDto.setData3(shopCommodityImages);

            return objectDto;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantException();
        }


    }

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public ShopMerchant readMerchantByShopId(String shopId) {

        try {
            //找到商品对象
            ShopCommodity shopCommodity = shopCommodityMapper.selectByPrimaryKey(shopId);
            //找到商家对象
            ShopMerchant shopMerchant = shopMerchantMapper.selectByPrimaryKey(shopCommodity.getMerchantId());

            return shopMerchant;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantException();
        }

    }
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public ShopMerchant readMerchantByMerchantId(String merchantId) {

        try {
            ShopMerchant shopMerchant = shopMerchantMapper.selectByPrimaryKey(merchantId);

            return shopMerchant;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantException();
        }
    }

    /**
    * @Author SooYoo
    * @Description
    * @Param 
    * @Return 
    **/
    @Override
    public List<ShopMerchant> readMerchantSimple(ShopMerchant shopMerchant) {

        List<ShopMerchant> shopMerchants = new ArrayList<>();

        ShopMerchant shopMerchantReality = shopMerchantMapper.selectByPrimaryKey(shopMerchant.getMerchantId());

        shopMerchants.add(shopMerchantReality);


        return shopMerchants;
    }

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public ShopMerchant readMerchantSimpleOne(ShopMerchant shopMerchant) {

        try {
            ShopMerchant shopMerchant1 = shopMerchantMapper.selectByPrimaryKey(shopMerchant.getMerchantId());

            return shopMerchant1;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantException();
        }
    }
}
